package com.example.ks.servlet;

import com.example.ks.dao.impl.OrderDaoImpl;
import com.example.ks.dao.impl.UserDaoImpl;
import com.example.ks.entity.Order;
import com.example.ks.entity.User;
import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.servlet.http.HttpSession;

import java.io.IOException;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@WebServlet("/admin-order")
public class AdminOrderServlet extends HttpServlet {

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // 从会话中获取当前登录的管理员信息
        HttpSession session = request.getSession();
        User admin = (User) session.getAttribute("user");
        int adminId = admin.getId();
        // 获取请求参数中的action值，用于判断查询类型
        String action = request.getParameter("action");
        try {
            OrderDaoImpl orderDao = new OrderDaoImpl();
            UserDaoImpl userDao = new UserDaoImpl();
            List<Order> orders;
            
            if ("search".equals(action)) {
                orders = orderDao.getOrdersByAdminId(adminId);
            } else {
                orders = orderDao.getAllOrders();
            }
            // 获取所有相关用户的用户名
            Map<Integer, String> userNames = new HashMap<>();
            for (Order order : orders) {
                if (!userNames.containsKey(order.getAdminId())) {
                    User adminUser = userDao.findById(order.getAdminId());
                    if (adminUser != null) {
                        userNames.put(order.getAdminId(), adminUser.getUsername());
                    }
                }
                if (!userNames.containsKey(order.getUserId())) {
                    User normalUser = userDao.findById(order.getUserId());
                    if (normalUser != null) {
                        userNames.put(order.getUserId(), normalUser.getUsername());
                    }
                }
            }
            request.setAttribute("orders", orders);
            request.setAttribute("userNames", userNames);
        } catch (SQLException e) {
            e.printStackTrace();
            request.setAttribute("error", "获取订单信息失败：数据库错误");
        }
        request.getRequestDispatcher("admin-index.jsp").forward(request, response);
    }

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        int orderId = Integer.parseInt(request.getParameter("orderId"));
        int status = Integer.parseInt(request.getParameter("status"));

        try {
            OrderDaoImpl orderDao = new OrderDaoImpl();
            int rowsAffected = orderDao.updateOrderStatus(orderId, status);
            if (rowsAffected > 0) {
                response.sendRedirect("admin-order?action=search");
            } else {
                request.setAttribute("error", "更新订单状态失败");
                request.getRequestDispatcher("admin-index.jsp").forward(request, response);
            }
        } catch (SQLException e) {
            e.printStackTrace();
            request.setAttribute("error", "更新订单状态失败：数据库错误");
            request.getRequestDispatcher("admin-index.jsp").forward(request, response);
        }
    }
}